$(function(){
	picStart("144002");
	embeddingunitnumberStart("144001");
	dlStart(companyId);
/*	$('#linecode').bind('change',function(){
		fallow();
	});*/
/*	$('#predevicetype').bind('change',function(){
		gtStart(companyId);
	});*/
	$('#picfile').bind('change',function(){
		getPicPath();
	});
/*	$('#devicename').bind('blur',function(){
		checkDeviceName();
	});*/
/*	$("#starttowercode").bind('change',function(){
		getTowername();
	});
	$("#endtowercode").bind('change',function(){
		getTowername();
	});*/
	$("#reflinecode").bind('change',function(){
		dlChange(companyId);
	});
	linelist();
});

function getPicPath(){
	$('#kzsx_add_pic_text').val($('#picfile').val());
}

function uploadPic(){
	var imgcount=$(".img_p").length+1;
	var pic='picfile';
	var picfile=$('#'+pic).val();
	var deviceName=$('#devicename').val();
	if(picfile==''){
		alert('请添加设备图片');
		return;
	}
	var photoType=picfile.substring(picfile.lastIndexOf("."),picfile.length).toUpperCase();
	//alert(photoType);
	if(photoType!=".JPG"&&photoType!=".GIF"){
		alert("添加的图片格式错误");
		return;
	}
	if(deviceName==''){
		alert("请输入设备名称");
		return;
	}
	if(imgcount>6){
		alert("添加图片已达极限");
		return;
	}
	changePicWidth();
	var pictype=$('#pictype').val();
	$.ajaxFileUpload({
		/*url : contextPath+"user/picture/upload?devicename="+deviceName+"&pictype="+pictype+"&imgcount="+imgcount,*/
		url : contextPath+"user/picture/upload",
		secureuri : false,
		fileElementId : pic,
		data : {data : [$('#'+pic).attr('name')],
				devicename : deviceName,
				pictype : pictype,
				imgcount : imgcount
		},
		dataType : 'json',
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success : function(data,status){
			//alert(data);
			if(data!=null){
				//alert(data.msgDesc);
				//$('#inner_box').css('width',120*imgcount+'px');
				var option="";
				option += '<div class="img_p"><img class="small_pic" id="number'+imgcount+'" src="'+basePath+'upload/'+data.resourcepicname+'" alt="" />';
				option +='<img class="img_del" onclick="imgdel(this)" src="'+basePath+'user/images/add_members_close.png" />';
				//option += '<div class="img_p"><div class="top_div"><img class="small_pic" onload="autoSize(this,98)" alt="" />';
				option += '<p>'+data.msg+'</p></div>';
				$('#clear').before(option);
				//alert(kzsx_img_cnt);
				if(imgcount>3){
					var kzsx_img_left=-120*(imgcount-3);
					$('.kzsx_cont_leftbox .img_flash_box .center .inner_box').stop().animate({'left':kzsx_img_left+'px'},200);
				}
			}
		}
	});
}

function getDeviceName(txt){
	var devicename=$('#devicename').val();
	if(devicename==""){
		return;
	}
	$.ajax({
		url : contextPath+"user/picture/devicename",
		data : {devicename : txt},
		type : 'post',
		success : function(){
			//alert(txt);
		}
	});
}

function isamorphous(){
	var amorphous=$("#model").val()+"";
	if(amorphous!=""&&amorphous!=null&&amorphous.indexOf('H')>=0){
		$("#isamorphous").val("是");
		$("#isamorphous1").val("是");
	}else{
		$("#isamorphous").val("否");
		$("#isamorphous1").val("否");
	}
}

function add(){
	var embeddingunitnumber="";
	var connectioncable="";
	var reflinecode="";
	$(".ccode").each(function(i){
		if(i==0){
			connectioncable = $(this).html();
		}else{
			connectioncable += ","+$(this).html();
		}
	});
	$("#ccode").val(connectioncable);
	$(".lcode").each(function(i){
		if(i==0){
			reflinecode = $(this).html();
		}else{
			reflinecode += ","+$(this).html();
		}
	});
	$("#lcode").val(reflinecode);
	$(".enumber").each(function(i){
		if(i==0){
			embeddingunitnumber = $(this).html();
		}else{
			embeddingunitnumber += ","+$(this).html();
		}
	});
	$("#enumber").val(embeddingunitnumber);
	var devicename=$("#devicename").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return;
	}
	if(!isVoltage($("input[name='voltagelevel']").val())){
		alert("电压等级不正确，如220v或1kv");
		return;
	}
	if(!isNum($("input[name='crossdistance']").val())){
		alert("交跨距离必须为小数");
		return;
	}
	if($("input[name='crossname']").val()==""){
		alert("被跨物名称为空");
		return;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zydlj/add",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				if($('#inner_box .img_p').length>0){
					var count=1;
					$('#inner_box .img_p p').each(function(){
						var picname=$(this).text();
						var resourcepicname=$("#number"+count).attr("src").substring($("#number"+count).attr("src").lastIndexOf("/")+1);
						var linecode=$('#linecode').val();
						var devicecode=data.deviceid;
						var devicename=$('#devicename').val();
						//alert(picname);
						$.ajax({
							url : contextPath+'user/picture/add',
							type : 'post',
							async:false,
							data : {
								picname : picname,
								resourcepicname : resourcepicname,
								linecode : linecode,
								deviceid : devicecode,
								devicename : devicename,
								devicetype : 123
							},
							success : function(data){
								//alert(data.msgDesc);
								count++;
							}
						});
					});
				}
				location.href=contextPath+"user/z/zydljlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function edite(){
	var did=$("input[name='deviceid']").val();
	setStatus(did);
	var devicename=$("#devicename").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	if($("#starttowercode").val()=="0"){
		$("#starttowercode").val("");
		$("#starttowername").val("");
	}
	if($("#endtowercode").val()=="0"){
		$("#endtowercode").val("");
		$("#endtowername").val("");
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return;
	}
	if(!isVoltage($("input[name='voltagelevel']").val())){
		alert("电压等级不正确，如220v或1kv");
		return;
	}
	if(!isNum($("input[name='crossdistance']").val())){
		alert("交跨距离必须为小数");
		return;
	}
	if($("input[name='crossname']").val()==""){
		alert("被跨物名称为空");
		return;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zydlj/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				if($('#inner_box .img_p').length>0){
					$('#inner_box .img_p p').each(function(){
						var picname=$(this).text();
						var linecode=$('#linecode').val();
						var devicecode=data.deviceid;
						//alert($(this).parent().children(".small_pic").attr("src").substring($(this).parent().children(".small_pic").attr("src").lastIndexOf("/")+1));
						var resourcepicname=$(this).parent().children(".small_pic").attr("src").substring($(this).parent().children(".small_pic").attr("src").lastIndexOf("/")+1);
						var devicename=$('#devicename').val();
						var pid=$(this).parent().children(".pid").val();
//						alert(pid);
						if(typeof(pid)=='undefined'){
							pid="";
						}
						//alert(picname);
						$.ajax({
							url : contextPath+'user/picture/edite',
							type : 'post',
							async: false,
							data : {
								picname : picname,
								resourcepicname : resourcepicname,
								linecode : linecode,
								deviceid : devicecode,
								devicename : devicename,
								pid : pid,
								devicetype : 123
							},
							success : function(){
							}
						});
					});
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/z/zydljlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
						}
					});
				}else{
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/z/zydljlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
						}
					});
				}
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}
function aa(){
	alert($("#endtowercode").val());
}

function getTowername(){
	$('#starttowername').val($('#starttowercode option[value='+$('#starttowercode').val()+']').text());
	$('#endtowername').val($('#endtowercode option[value='+$('#endtowercode').val()+']').text());
}

function addRef(){
	$("#addRef").show();
}
var embeddingunitnumberStart=function(typeId){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : typeId
		},
		type : 'POST',
		success:function(list){
			//alert(list.length);
			if(null!=list && list.length>0){
				$("#embeddingunitnumber").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				$('#embeddingunitnumber').append(option);
			}
		}
	});
};
var dllist;
var llist;
var firstLine="";
var icount=0;
var dlStart=function(companyId){
	$.ajax({
		url : contextPath+"user/zyxl/dlist",
		data : {
			companyId : companyId,
			transformercode : transformercode
		},
		type : 'POST',
		success:function(list){
			llist = list;
			if(null!=list && list.length>0){
				firstLine = list[0].deviceid;
				$("#connectioncable").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					$.ajax({
						url : contextPath+'user/zydl/dlist',
						type : 'post',
						data : {
							companyId : companyId,
							deviceid : list[i].deviceid
						},
						success : function(data){
							if(data!=null&&data.length>0){
								if(icount==0){
									dllist = data;
								}else{
									dllist = dllist.concat(data);
								}
								icount++;
								var dllinecode = "";
								for(var j=0;j<data.length;j++){
									dllinecode= data[0].linecode;
									option += '<option value="'+data[j].deviceid+'">'+data[j].devicename+'</option>';
								}
								if(dllinecode==firstLine){
									$('#connectioncable').append(option);
								}
								option="";
							}
						}
					});
				}
				if(isedit){
					goToPage(1);
				}
			}
		}
	});
};
function linelist(){
	$.ajax({
		url : contextPath+'user/zyxl/dlist',
		data : {
			companyId : companyId,
			transformercode : transformercode
		},
		type : 'post',
		async:false,
		success : function(data){
			if(data!=null&&data.length>0){
				$("#reflinecode").empty().show();
				var option="";
				for(var i=0;i<data.length;i++){
					option += '<option value="'+data[i].deviceid+'">'+data[i].devicename+'</option>';
				}
				$("#reflinecode").append(option);
				//$('#linecode').val(deviceid);
			}
		}
	});
}
var dlChange=function(companyId){
	var reflinecode = $("#reflinecode").val();
	$("#connectioncable").empty().show();
	var option="";
	$.ajax({
		url : contextPath+'user/zydl/dlist',
		type : 'post',
		data : {
			companyId : companyId,
			deviceid : reflinecode
		},
		success : function(data){
			if(data!=null&&data.length>0){
				for(var j=0;j<data.length;j++){
					option += '<option value="'+data[j].deviceid+'">'+data[j].devicename+'</option>';
				}
				$('#connectioncable').append(option);
			}
		}
	});
};
var count=1;
function savaRef(){
	var embeddingunitnumber = $("#embeddingunitnumber").val();
	var reflinecode = $("#reflinecode").val();
	var reflinename = "";
	for(var i=0;i<llist.length;i++){
		if(reflinecode==llist[i].deviceid){
			reflinename = llist[i].devicename;
		}
	}
	var connectioncable = $("#connectioncable").val();
	var connectioncableName="";
	for(var i=0;i<dllist.length;i++){
		if(connectioncable==dllist[i].deviceid){
			connectioncableName = dllist[i].devicename;
		}
	}
	//alert($(".enumber").serializeArray());
	var bl=true;
	if(embeddingunitnumber=='' || embeddingunitnumber==null){
		alert("请选择埋设单元号");
		bl=false;
	}
	if(connectioncable=='' || connectioncable==null){
		alert("请选择关联电缆");
		bl=false;
	}
	$(".ccode").each(function(){
		if($(this).html()==connectioncable){
			alert(connectioncableName+"已存在关联关系");
			bl=false;
		}
	});
	if(bl){
		var ul='<ul class="byq_cont_ul"><li style="width:5%;">'+count+'</li>'
			+'<li  class="enumber"  style="width:10%;">'+embeddingunitnumber+'</li>'
			+'<li style="width:25%;">'+reflinename+'<span class="lcode" style="display: none;" >'+reflinecode+'</span></li>'
			+'<li style="width:40%;">'+connectioncableName+'<span class="ccode" style="display: none;" >'+connectioncable+'</span></li>'
			+'<li class="operate" style="width:14%; border-right:none;"><a onclick="delRef(this);" class="detail_a"> 删除 </a></li></ul>';
		count++;
		$("#reflist").append(ul);
	}
}
function savaRefById(){
	var deviceid = $("#deviceid").val();
	var linecode = $("#reflinecode").val();
	var embeddingunitnumber = $("#embeddingunitnumber").val();
	var connectioncable = $("#connectioncable").val();
	var bl=true;
	if(embeddingunitnumber=='' || embeddingunitnumber==null){
		alert("请选择埋设单元号");
		bl=false;
	}
	if(connectioncable=='' || connectioncable==null){
		alert("请选择关联电缆");
		bl=false;
	}
	if(bl){
		$.ajax({
			url : contextPath+'user/zydljdlref/add',
			type : 'post',
			data : {
				deviceid : deviceid,
				embeddingunitnumber:embeddingunitnumber,
				connectioncable:connectioncable,
				linecode:linecode
			},
			success : function(data){
				alert(data.msgDesc);
				if(data.opResult==0)
					goToPage(1);
			}
		});
	}
}
function delRef(obj){
	$(obj).parent().parent().remove();
}
function delRefById(id){
	$.ajax({
		url : contextPath+'user/zydljdlref/delete',
		type : 'post',
		data : {
			deviceid : id
		},
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult==0)
				goToPage(1);
		}
	});
}

var goToPage=function(pageNo){
	var deviceid = $("#deviceid").val();
	$.ajax({
		url : contextPath+"user/zydljdlref/list",
		data : {
			deviceid : deviceid,
			pageSize:5,
			pageNo : pageNo
		},
		type : 'POST',
		success:function(list){
			$("#reflist").empty();
			$(".page").empty();
			if(list!=null && list.totalRecord>0){
				var obj = list.records;
				for(var i=0;i<obj.length;i++){
					var connectioncable =obj[i].connectioncable;
					var connectioncableName="";
					for(var j=0;j<dllist.length;j++){
						if(connectioncable==dllist[j].deviceid){
							connectioncableName = dllist[j].devicename;
							break;
						}
					}
					var lcode =obj[i].linecode;
					var lname="";
					for(var j=0;j<llist.length;j++){
						if(lcode==llist[j].deviceid){
							lname = llist[j].devicename;
							break;
						}
					}
					var ul='<ul class="byq_cont_ul"><li style="width:5%;">'+(i+1)+'</li>'
					+'<li  class="enumber"  style="width:10%;">'+obj[i].embeddingunitnumber+'</li>'
					+'<li  class="enumber"  style="width:25%;">'+lname+'</li>'
					+'<li style="width:40%;">'+connectioncableName+'</li>'
					+'<li class="operate" style="width:14%; border-right:none;"><a onclick="delRefById(\''+obj[i].id+'\');" class="detail_a"> 删除 </a></li></ul>';
					$("#reflist").append(ul);
				}
				pageList(list);
			}
		}
	});
};